<?php
include_once("../lang/$langs/company_type_config.php");
//==================================
if(!empty($_POST["action"]))
{
	$_POST['rank']=empty($_POST['rank'])?0:$_POST['rank'];
	$_POST['tj']=empty($_POST['tj'])?0:$_POST['tj'];
	$_POST['stime']=strtotime($_POST['stime']);
	$_POST['etime']=strtotime($_POST['etime']);
	$sql="select userid,email,username from ".MEMBER." where userid='$_GET[userid]' ";
	$db->query($sql);
	$uid=$db->fetchRow();
	if(!empty($_POST['password'])) 
	{
		$sql="update ".MEMBER." set password='". substr(hash('sha512', 'password_' . $_POST['password']), 0, 32)."' where userid='$_GET[userid]'";
		$db->query($sql);
	}
	$sql="update ".MEMBER." set status='$_POST[statu]',mobile='$_POST[mobile]',qq='{$_POST['qq']}',weixin='{$_POST['weixin']}',name='$_POST[name]',email='$_POST[email]',signature='$_POST[signature]',birthday='$_POST[birthday]' where userid='$_GET[userid]'";
	$db->query($sql);
	
	//绑定圈子勋章
	$sql = "select * from ".SNSMEMBERMEDAL." where member_id='$_GET[userid]'";
	$db->query($sql);
	$member_medals = $db->getRows();
	$mmids = array();
	$medal_ids = array();
	if(!empty($member_medals)){
		foreach ($member_medals as $vv)
		{
			$mmids[] = $vv['id'];
			$medal_ids[$vv['id']] = $vv['medal_id'];
		}
	}
	if(!empty($_POST['medal'])){
		$insertSql = "insert into ".SNSMEMBERMEDAL." (member_id,medal_id,create_time) values ";
		$sqlstr = "";
		$max_medal_id = 0;
		foreach ($_POST['medal'] as $mv)
		{
			$sql = "select * from ".SNSMEMBERMEDAL." where member_id='$_GET[userid]' and medal_id='$mv'";
			$db->query($sql);
			$member_medal = $db->fetchRow();
			if(empty($member_medal)){
				$sqlstr .= "('$_GET[userid]','$mv','".date('Y-m-d H:i:s',time())."'),";
			}
			if($mv>$max_medal_id){
				$max_medal_id = $mv;
			}
		}
		if(!empty($medal_ids)){
			foreach ($medal_ids as $mik=>$miv)
			{
				if(in_array($miv, $_POST['medal'])){
					unset($medal_ids[$mik]);
				}
			}
			if(!empty($medal_ids)){
				$medal_ids = array_flip($medal_ids);
				$sql = "delete from ".SNSMEMBERMEDAL." where id in(".implode(',', $medal_ids).")";
				$db->query($sql);
			}
		}
		if($sqlstr!=""){
			$db->query($insertSql.rtrim($sqlstr,","));
			
			$sql = "select logo from ".SNSMEDAL." where id='".$max_medal_id."'";
			$db->query($sql);
			$medal_logo = $db->fetchField('logo');
			if($medal_logo!=""){
				$sql = "update ".MEMBER." set sns_medal_logo='".$medal_logo."' where userid='".$_GET['userid']."'";
				$db->query($sql);
			}
		}
	}else{
		if(!empty($mmids)){
			$sql = "delete from ".SNSMEMBERMEDAL." where id in(".implode(',', $mmids).")";
			$db->query($sql);
		}
	}
	
	//-------------邮件发送-----------
	if(!empty($_POST['send_mail']))
	{
		$ar1=array('[username]','[weburl]');
		$ar2=array($uid['username'],$config['weburl']);
		$con=str_replace($ar1,$ar2,$_POST['mail_con']);
		send_mail($uid['email'],$uid['username'],$_POST['mail_title'],$con);
	}
	//-------------------------------
	
	unset($_GET['id']);
	unset($_GET['s']);
	unset($_GET['m']);
	msg("?m=member&s=member.php&".implode('&',convert($_GET)));
}
//=====================================================
$sql="select * from ".MEMBER." where userid='$_GET[userid]'";
$db->query($sql);
$re=$db->fetchRow();

//勋章信息
$sql="select id,name from ".SNSMEDAL;
$db->query($sql);
$medal_list = $db->getRows();
//已绑定的勋章
$sql="select medal_id from ".SNSMEMBERMEDAL." where member_id='$_GET[userid]'";
$db->query($sql);
$check_medal_list = $db->getRows();
$check_medal_ids = array();
foreach ($check_medal_list as $cmlv)
{
	$check_medal_ids[] = $cmlv['medal_id'];
}

$ip=$re['ip'];
$lastLoginTime=empty($re['lastLoginTime'])?time():$re['lastLoginTime'];
$regtime=empty($re['regtime'])?date("Y-m-d",$lastLoginTime):$re['regtime'];
$statu=$re['status'];
$point=$re['points'];
//========================================
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="guidebox"><?php echo lang_show('system_setting_home');?> &raquo; <?php echo lang_show('mem_modify_info');?></div>
<div class="bigbox">
	<div class="bigboxhead"><?php echo lang_show('mem_modify_info');?></div>
	<div class="bigboxbody">
      <form method="post" action=""  name=cityform>
        <table width='98%' border='0' cellspacing='0' cellpadding='0' align="center" class="menu">
          <tr>
		    <td height="24" align="left"><?php echo lang_show('user_id');?></td>
		    <td><?php echo $re['username']?></td>
	      </tr>
          <tr>
            <td height="24" align="left"><?php echo lang_show('regip');?></td>
            <td>
			<?php 
			if($ip)
			{	
				echo $ip;
				$ipfile="../lib/tinyipdata.dat";
				echo $from = convertip($ip, $ipfile);
			}
			?></td>
          </tr>
          <tr>
            <td height="24" align="left"><?php echo lang_show('reg_time');?></td>
            <td><?php echo "$regtime"; ?></td>
          </tr>
          <tr>
            <td height="24" align="left"><?php echo lang_show('login_type');?></td>
            <td><?php echo $re['open_type']?$open_type[$re['open_type']]:"自营"; ?></td>
          </tr>
          <tr>
            <td height="24" align="left"><?php echo lang_show('client_platform');?></td>
            <td><?php echo $re['client_platform']?$client_platform[$re['client_platform']]:"未知"; ?></td>
          </tr>
          <tr>
            <td height="24" align="left"><?php echo lang_show('client_system');?></td>
            <td><?php echo $re['client_system']?$client_platform[$re['client_system']]:"未知"; ?></td>
          </tr>
          <tr>
            <td height="24" align="left">最后登录时间</td>
            <td><?php echo date("Y-m-d H:i:s",$lastLoginTime) ;?></td>
          </tr>
          <tr> 
			<td width='15%' height="24" align="left"><?php echo lang_show('statu');?></td>
			<td width='92%'>
			  <select class="select" name="statu">
				<?php
				foreach($member_group as $key=>$v)
				{
				?>
				<option value="<?php echo $key; ?>" <?php if($statu==$key) echo "selected"; ?>><?php echo $v; ?></option>
				<?php
				 }
				?>
			  </select>
			  </td>
          </tr>
		  <tr>
		    <td height="24" align="left"><?php echo lang_show('mod_password');?></td>
		    <td><input class="text" type="text" name="password" id="password"></td>
	      </tr>
	      <tr>
		    <td height="24" align="left"><?php echo lang_show('contact');?></td>
		    <td><input class="text" type="text" name="name" id="name" value="<?php echo $re['name']?>"></td>
	      </tr>
	      <tr>
		    <td height="24" align="left">个性签名</td>
		    <td><input class="text" type="text" name="signature" id="signature" value="<?php echo $re['signature']?>"></td>
	      </tr>
	      <tr>
		    <td height="24" align="left"><?php echo lang_show('mobile');?></td>
		    <td><input class="text" type="text" name="mobile" id="mobile" value="<?php echo $re['mobile']?>"></td>
	      </tr>
	      <tr>
		    <td height="24" align="left">QQ</td>
		    <td><input class="text" type="text" name="qq" id="qq" value="<?php echo $re['qq']?>"></td>
	      </tr>
	      <tr>
		    <td height="24" align="left">微信</td>
		    <td><input class="text" type="text" name="weixin" id="weixin" value="<?php echo $re['weixin']?>"></td>
	      </tr>
		  <tr>
		    <td height="24" align="left">注册邮箱</td>
		    <td><input class="text"  name="email" type="text" value="<?php echo $re['email'];?>" /></td>
	      </tr>
	  	  <tr>
		    <td height="24" align="left">生日</td>
		    <td><input class="text"  name="birthday" type="text" value="<?php echo $re['birthday'];?>" />如1980-08-05</td>
	      </tr>    
		  <tr> 
			<td width='15%' height="24" align="left"><?php echo lang_show('mem_credit');?></td>
			<td width='92%'>
			<?php if(!empty($re['points'])) echo $re['points']; else echo 0;?> <a href="javascript:;" onclick="window.parent.iframe_form('unify','增减积分','module.php?m=member&s=member_unify.php&type=changepoints&member_id=<?php echo $_GET[userid];?>',400,200);">增减积分</a>	<a href="javascript:;" onclick="window.parent.iframe_form('unify','查看积分详情','module.php?m=member&s=member_unify.php&type=showpointslogs&member_id=<?php echo $_GET[userid];?>',400,300);">查看明细</a>	</td>
          </tr>
		  <tr> 
			<td width='15%' height="24" align="left">圈子勋章</td>
			<td width='92%'>
			<?php 
			foreach ($medal_list as $mlv)
			{
			?>
			<input name="medal[]" type="checkbox" value="<?php echo $mlv['id'];?>" <?php if(in_array($mlv['id'], $check_medal_ids)) echo "checked";?>><?php echo $mlv['name'];?>
			<?php 
			}
			?>
			</td>
          </tr>
          <tr>
            <td height="24">邮件通知</td>
            <td>
				<input class="text" value="<?php $mail_temp=get_mail_template('review_user'); echo $mail_temp['subject']; ?>" name="mail_title" /><br />
				<textarea style="margin-top:5px;" name="mail_con" class="text" cols="" rows="8"><?php echo $mail_temp['message'];?></textarea>
				<br />
				<input type="checkbox" class="checkbox" name="send_mail" value="1" />
				<span class="bz">发邮件通知会员</span>
				</td>
          </tr>
          <tr> 
			<td width='15%' height="24">&nbsp;</td>
			<td width='92%'> 
			  <input class="btn" style="width:50px;" type='submit' name='submit' value='<?php echo lang_show('submit');?>'>
			  <input name="action" type="hidden" id="action" value="submit">			</td>
          </tr>
        </table>
      </form>
	</div>
</div>
</body>
</html>